import os

# 导入requests包
import requests

from bs4 import BeautifulSoup

import OperationMysql


class WallPaperSpider:

    def __init__(self):  # 类的初始化操作
        self.headers = {  # 给请求指定一个请求头来模拟chrome浏览器
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        }
        self.web_url = 'https://wallhaven.cc/toplist?page='  # 要访问的网页地址
        self.folder_path = 'D:\python_project\wallpaper'  # 设置图片要存放的文件目录

    def getHtml(self, url):  # 请求网页，返回网页html数据
        r = requests.get(url, headers=self.headers)  # 像目标url地址发送get请求，返回一个response对象。有没有headers参数都可以。
        return r

    def getImgUrl(self):  # 执行操作
        print("执行网络请求:")
        img_list = []
        for i in range(2, 100):
            r = self.getHtml(self.web_url + str(i))
            print("开始获取第" + str(i) + "页img标签")
            all_img = BeautifulSoup(r.text, 'lxml').find_all('img', class_='lazyload')  # 获取网页中的class为lazyload的所有a标签
            for img in all_img:
                ms = OperationMysql.OperationMysql()
                thumb_url = img['data-src']
                th = str(thumb_url)
                img_id = th[th.rfind('/') + 1:th.rfind('.')]
                web_url = "https://wallhaven.cc/w/" + img_id
                result = self.getHtml(web_url)
                img_detail = BeautifulSoup(result.text, 'lxml').find('img', id='wallpaper')
                if img_detail:
                    img_detail = str(img_detail)
                    img_info = img_detail.split("\"")[1]
                    img_url = img_detail.split("\"")[13]
                    img_width = int(img_detail.split("\"")[9])
                    img_height = int(img_detail.split("\"")[5])
                    ms.insert_one(
                        "insert into b_image (web_url, thumbnail_url, img_url, img_info, width, height) values ('%s', '%s', '%s', '%s','%d','%d')" % (
                        web_url, thumb_url, img_url, img_info, img_width, img_height))


wall = WallPaperSpider()
wall.getImgUrl()
